package com.ledi.guns.modular.system.controller;

import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ledi.guns.common.constant.cache.Cache;
import com.ledi.guns.common.constant.factory.ConstantFactory;
import com.ledi.guns.common.exception.BizExceptionEnum;
import com.ledi.guns.common.exception.BussinessException;
import com.ledi.guns.common.persistence.dao.RoutesMapper;
import com.ledi.guns.common.persistence.model.Routes;
import com.ledi.guns.core.base.controller.BaseController;
import com.ledi.guns.core.base.tips.SuccessTip;
import com.ledi.guns.core.base.tips.Tip;
import com.ledi.guns.core.cache.CacheKit;
import com.ledi.guns.core.node.ZTreeNode;
import com.ledi.guns.core.util.ToolUtil;
import com.ledi.guns.modular.system.service.IRoutesService;
import com.ledi.guns.modular.system.warpper.RoutesWarpper;

/**
 * routes控制器
 *
 * @author fengshuonan
 * @Date 2018-03-20 11:18:25
 */
@Controller
@RequestMapping("/routes")
public class RoutesController extends BaseController {

	private String PREFIX = "/system/routes/";

	@Autowired
	private RoutesMapper routesMapper;

	@Autowired
	private IRoutesService iRoutesService;

	/**
	 * 跳转到routes首页
	 */
	@RequestMapping("")
	public String index() {

		return PREFIX + "routes.html";
	}

	/**
	 * 跳转到添加routes
	 */
	@RequestMapping("/routes_add")
	public String routesAdd() {

		return PREFIX + "routes_add.html";
	}

	/**
	 * 跳转到修改routes
	 */
	@RequestMapping("/routes_edit/{routesId}")
	public String routesUpdate(@PathVariable Integer routesId, Model model) {

		if (ToolUtil.isEmpty(routesId)) {
			throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
		}
		Routes routes = routesMapper.selectById(routesId);
		model.addAttribute(routes);
		model.addAttribute("pName", ConstantFactory.me().getSingleRoutesName(routes.getPid()));
		return PREFIX + "routes_edit.html";
	}

	/**
	 * 路由修改
	 */
	@RequestMapping(value = "/update")
	@ResponseBody
	public Tip edit(@Valid Routes routes, BindingResult result) {

		if (result.hasErrors()) {
			throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
		}
		routesMapper.updateById(routes);

		// 删除缓存
		CacheKit.removeAll(Cache.CONSTANT);
		return SuccessTip.buildSuccess();
	}

	/**
	 * 获取路由列表
	 */
	@RequestMapping(value = "/routesTreeList")
	@ResponseBody
	public List<ZTreeNode> roleTreeList() {

		List<ZTreeNode> roleTreeList = routesMapper.routesTreeList();
		roleTreeList.add(ZTreeNode.createParent());
		return roleTreeList;
	}

	/**
	 * 获取routes列表
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public Object list(@RequestParam(required = false) String routesName) {

		List<Map<String, Object>> routes = routesMapper.selectRoutes(super.getPara("routesName"));
		return super.warpObject(new RoutesWarpper(routes));
	}

	/**
	 * 新增routes
	 */
	@RequestMapping(value = "/add")
	@ResponseBody
	public Object add(@Valid Routes routes, BindingResult result) {

		if (result.hasErrors()) {
			throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
		}
		routes.setId(null);
		// 设置父级编号
		routesSetPcode(routes);
		routesMapper.insert(routes);
		return SuccessTip.buildSuccess();
	}

	private void routesSetPcode(Routes routes) {

		if (ToolUtil.isEmpty(routes.getPid()) || routes.getPid().equals(0L)) {
			routes.setPcode("0");

		} else {
			Long code = routes.getPid();
			Routes pRoutes = routesMapper.selectById(code);
			routes.setPcode(pRoutes.getCode());
			// 如果编号和父编号一致会导致无限递归
			if (routes.getCode().equals(routes.getPcode())) {
				throw new BussinessException(BizExceptionEnum.MENU_PCODE_COINCIDENCE);
			}
		}

	}

	/**
	 * 获取路由列表
	 */
	@RequestMapping(value = "/routesTreeListByRoleId/{roleId}")
	@ResponseBody
	public List<ZTreeNode> routesTreeListByRoleId(@PathVariable Integer roleId) {

		List<Integer> routesIds = routesMapper.getRoutesIdsByRoleId(roleId);
		if (ToolUtil.isEmpty(routesIds)) {
			List<ZTreeNode> roleTreeList = routesMapper.routesTreeList();
			return roleTreeList;
		} else {
			List<ZTreeNode> roleTreeListByUserId = routesMapper.routesTreeListByRoutesIds(routesIds);
			return roleTreeListByUserId;
		}
	}

	/**
	 * 删除routes
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Object delete(@RequestParam Long routesId) {

		if (ToolUtil.isEmpty(routesId)) {
			throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
		}
		iRoutesService.delRoutesById(routesId);
		// 删除缓存
		CacheKit.removeAll(Cache.CONSTANT);
		return SuccessTip.buildSuccess();
	}

	/**
	 * routes详情
	 */
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Object detail(@RequestParam Long routesId) {

		if (ToolUtil.isEmpty(routesId)) {
			throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
		}
		routesMapper.selectById(routesId);
		return SuccessTip.buildSuccess();
	}
}
